package com.loveprogrammer.pool2.demo1;

import com.alibaba.fastjson.JSONObject;

import static java.lang.Thread.sleep;

/**
 * @ClassName DemoTest
 * @Description TODO
 * @Author admin
 * @Date 2023/11/7 9:48
 * @Version 1.0
 */
public class DemoTest {

    public static void main(String[] args) throws Exception {
        // 最大对象数量3 最小空闲对象数量1 最大空闲数量 2
        ConnectorPool myObjectPool = ConnectorPool.INSTANCE;
        System.out.println(myObjectPool.getNumActive());
        // 获取对象
        Connector obj = myObjectPool.borrowObject();
        // 获取对象
        Connector obj2 = myObjectPool.borrowObject();
        // 获取对象
        Connector obj3 = myObjectPool.borrowObject();

        // 获取对象 已超出最大对象数
        // Connector obj4 = myObjectPool.borrowObject();

        //归还对象
        myObjectPool.returnObject(obj);
        System.out.println("ThreadName:" + Thread.currentThread().getName() + " returned: " + JSONObject.toJSONString(obj));
        sleep(1000);
        System.out.println(myObjectPool.getNumActive());

    }
}
